﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Text;

namespace 花店管理系统.Customer_Services
{
    public partial class Client : Form
    {
        // 新增字体大小变量
        private readonly Font sendFont = new Font("Arial", 14, FontStyle.Bold); // 示例：使用Arial字体，大小为14，加粗
        public Client()
        {
            InitializeComponent();
            LoadConversation();
            // 初始化自定义字体
            rtbConversation.Font = new Font("Arial", 15); // 设置默认字体，可根据需要调整
        }
        public void RefreshConversation()
        {
            rtbConversation.Clear();//清空聊天记录
            LoadConversation();//重新加载聊天记录
        }
        private void LoadConversation()
        {
            rtbConversation.Clear();
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["花店管理系统.Properties.Settings.huadianConnectionString"].ConnectionString))
            {

                connection.Open();
                string query = "SELECT Sender, MessageContent FROM Messages WHERE UserID=@UserID ORDER BY SendTime";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@UserID", Login.t_id);
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string sender = reader["Sender"].ToString();
                            string messageContent = reader["MessageContent"].ToString();

                            AppendMessage(sender, messageContent);
                                                         // rtbConversation.AppendText(sender + ": " + messageContent + Environment.NewLine);
                        }
                    }
                }
            }
        }

      

        private void SaveMessageToDatabase(string sender, string messageContent)
        {
            rtbConversation.Clear();
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["花店管理系统.Properties.Settings.huadianConnectionString"].ConnectionString))
            {
                connection.Open();
                string query = "INSERT INTO Messages (UserID,Sender, MessageContent, SendTime) VALUES (@UserID,@Sender, @MessageContent, @SendTime)";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@UserID", Login.t_id);
                    command.Parameters.AddWithValue("@Sender", sender);
                    command.Parameters.AddWithValue("@MessageContent", messageContent);
                    command.Parameters.AddWithValue("@SendTime", DateTime.Now);
                    command.ExecuteNonQuery();
                }
            }
        }
        private void AppendMessage(string sender, string messageContent, bool isSender = false)
        {
            if (isSender)
            {
                rtbConversation.SelectionFont = sendFont; // 使用定义好的发送者字体
                rtbConversation.SelectionAlignment = HorizontalAlignment.Right;
                rtbConversation.SelectionColor = Color.Blue;//发送消息颜色
            }
            else
            {
                rtbConversation.SelectionFont = rtbConversation.Font; // 使用默认字体（接收方）
                rtbConversation.SelectionAlignment = HorizontalAlignment.Left;
                rtbConversation.SelectionColor = Color.Red;//接受方消息颜色
            }
            rtbConversation.AppendText(sender + ":" + messageContent + Environment.NewLine);
            rtbConversation.ScrollToCaret();
        }
        private void btnSendReply_Click(object sender, EventArgs e)
        {
            if (txtReplyMessage.Text == "")
            {
                MessageBox.Show("发送内容不能为空,请至少包含一个字符!");
            }
            else
            {
                string replyMessage = txtReplyMessage.Text;
                string s = Login.username;
                SaveMessageToDatabase(s, replyMessage);
                LoadConversation();
                txtReplyMessage.Clear();
            }


        }

        private void Client_Load(object sender, EventArgs e)
        {
            rtbConversation.Clear();
            string imagePath = Login.GetUserImagePath(Login.username);

            if (!string.IsNullOrEmpty(imagePath) && File.Exists(imagePath))
            {
                pictureBox1.ImageLocation = imagePath; // 假设pictureBoxUser是用于显示用户图片的PictureBox控件
               
            }
            else
            {
                MessageBox.Show("用户图片不存在或路径无效。");
                // 可能需要设定默认图片或者做其他处理
            }
        }

        private void rtbConversation_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        

    }
}
